package mule;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

/**
 * Shows a window when a player accesses the Town tile
 * 
 */
public class TownFrame extends JFrame {
	Player player;
	JButton store, pub, assay, landOffices;
	Random rand;
	static boolean storeInitialized = false;

	static JButton buyFood, sellFood, buyEnergy, sellEnergy, buyOre, sellOre,
			buyCrystite, sellCrystite, buyFoodMule, sellFoodMule,
			buyEnergyMule, sellEnergyMule, buyOreMule, sellOreMule;

	static JLabel storeFood, storeEnergy, storeOre, storeCrystite,
			storeFoodMule, storeEnergyMule, storeOreMule, foodPrice,
			energyPrice, orePrice, crystitePrice, foodMulePrice,
			energyMulePrice, oreMulePrice;

	static int numFood, numEnergy, numOre, numCrystite, numMule;
	
	final int FOOD_PRICE = 30;
	final int ENERGY_PRICE = 25;
	final int ORE_PRICE = 50;
	final int CRYSTITE_PRICE = 100;
	final int FOOD_MULE = 125;
	final int ENERGY_MULE = 150;
	final int ORE_MULE = 175;

	/**
	 * Displays a window with 4 buttons for the 4 different functions of the
	 * Town tile
	 */
	public TownFrame() {
		
		
		player = Game.getCurrentPlayer();
		rand = new Random();
		setTitle("Town");
		store = new JButton();
		store.setIcon(new ImageIcon("src/mule/images/store.png"));
		store.setActionCommand("Store");
		pub = new JButton();
		pub.setIcon(new ImageIcon("src/mule/images/pub.png"));
		pub.setActionCommand("Pub");
		assay = new JButton();
		assay.setIcon(new ImageIcon("src/mule/images/assay.png"));
		assay.setActionCommand("Assay");
		landOffices = new JButton();
		landOffices.setIcon(new ImageIcon("src/mule/images/land.png"));
		landOffices.setActionCommand("Land Offices");
		pub.addActionListener(new action());
		store.addActionListener(new action());
		landOffices.addActionListener(new action());
		add(store);
		add(pub);
		add(assay);
		add(landOffices);
		setLayout(new GridLayout(4, 1));

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setSize(90, 560);
		setVisible(true);
		setLocationRelativeTo(null);
		setResizable(false);
		
	}

	class action implements ActionListener {
		/**
		 * Handles Pub and Store interaction with player
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			// Gambles when the player clicks on the Pub button
			if (e.getActionCommand().equals("Pub")) {
				player.gamble(Game.time, Game.roundNumber);
				Game.switchPlayer();
				Game.bottomPanel.updateInfo();
				dispose();
				Game.time = 50;
			}
			if (e.getActionCommand().equals("Land Offices")) {
				System.out.println("Land Office");
				Map.landOfficePhase = true;
				dispose();
			}
			if (e.getActionCommand().equals("Store")) {
				StoreFrame store = new StoreFrame();
			}
			if (e.getActionCommand().equals("Buy 1 Food")) {
				if (player.getMoney() >= FOOD_PRICE && numFood >= 1) {
					player.setMoney(player.getMoney() - FOOD_PRICE);
					player.setFood(player.getFood() + 1);
					numFood--;
					storeFood.setText("Amount of Food: " + numFood);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Sell 1 Food")) {
				if (player.getFood() >= 1) {
					player.setMoney(player.getMoney() + FOOD_PRICE);
					player.setFood(player.getFood() - 1);
					numFood++;
					storeFood.setText("Amount of Food: " + numFood);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Buy 1 Energy")) {
				if (player.getMoney() >= ENERGY_PRICE && numEnergy >= 1) {
					player.setMoney(player.getMoney() - ENERGY_PRICE);
					player.setEnergy(player.getEnergy() + 1);
					numEnergy--;
					storeEnergy.setText("Amount of Energy: " + numEnergy);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Sell 1 Energy")) {
				if (player.getEnergy() >= 1) {
					player.setMoney(player.getMoney() + ENERGY_PRICE);
					player.setEnergy(player.getEnergy() - 1);
					numEnergy++;
					storeEnergy.setText("Amount of Energy: " + numEnergy);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Buy 1 Ore")) {
				if (player.getMoney() >= ORE_PRICE && numOre >= 1) {
					player.setMoney(player.getMoney() - ORE_PRICE);
					player.setOre(player.getOre() + 1);
					numOre--;
					storeOre.setText("Amount of Ore: " + numOre);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Sell 1 Ore")) {
				if (player.getOre() >= 1) {
					player.setMoney(player.getMoney() + ORE_PRICE);
					player.setOre(player.getOre() - 1);
					numOre++;
					storeOre.setText("Amount of Ore: " + numOre);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Buy 1 Crystite")) {
				if (player.getMoney() >= CRYSTITE_PRICE && numCrystite >= 1) {
					player.setMoney(player.getMoney() - CRYSTITE_PRICE);
					player.setCrystite(player.getCrystite() + 1);
					numCrystite--;
					storeCrystite.setText("Amount of Crystite: " + numCrystite);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Sell 1 Crystite")) {
				if (player.getCrystite() >= 1) {
					player.setMoney(player.getMoney() + CRYSTITE_PRICE);
					player.setCrystite(player.getCrystite() - 1);
					numCrystite++;
					storeCrystite.setText("Amount of Crystite: " + numCrystite);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Buy Food Mule")) {
				if (!player.hasMule() && player.getMoney() >= FOOD_MULE) {
					player.setMoney(player.getMoney() - FOOD_MULE);
					player.setMule(new Mule("Food Mule"));
					numMule--;
					storeFoodMule.setText("Amount of total Mules: " + numMule);
					storeEnergyMule
							.setText("Amount of total Mules: " + numMule);
					storeOreMule.setText("Amount of total Mules: " + numMule);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Sell Food Mule")) {
				if (player.hasMule()
						&& player.getMule().toString().equals("Food Mule")) {
					player.setMoney(player.getMoney() + FOOD_MULE);
					player.setMule(null);
					numMule++;
					storeFoodMule.setText("Amount of total Mules: " + numMule);
					storeEnergyMule
							.setText("Amount of total Mules: " + numMule);
					storeOreMule.setText("Amount of total Mules: " + numMule);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Buy Energy Mule")) {
				if (!player.hasMule() && player.getMoney() >= ENERGY_MULE) {
					player.setMoney(player.getMoney() - ENERGY_MULE);
					player.setMule(new Mule("Energy Mule"));
					numMule--;
					storeFoodMule.setText("Amount of total Mules: " + numMule);
					storeEnergyMule
							.setText("Amount of total Mules: " + numMule);
					storeOreMule.setText("Amount of total Mules: " + numMule);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Sell Energy Mule")) {
				if (player.hasMule()
						&& player.getMule().toString().equals("Energy Mule")) {
					player.setMoney(player.getMoney() + ENERGY_MULE);
					player.setMule(null);
					numMule++;
					storeFoodMule.setText("Amount of total Mules: " + numMule);
					storeEnergyMule
							.setText("Amount of total Mules: " + numMule);
					storeOreMule.setText("Amount of total Mules: " + numMule);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Buy Ore Mule")) {
				if (!player.hasMule() && player.getMoney() >= ORE_MULE) {
					player.setMoney(player.getMoney() - ORE_MULE);
					player.setMule(new Mule("Ore Mule"));
					numMule--;
					storeFoodMule.setText("Amount of total Mules: " + numMule);
					storeEnergyMule
							.setText("Amount of total Mules: " + numMule);
					storeOreMule.setText("Amount of total Mules: " + numMule);
					Game.bottomPanel.updateInfo();
				}
			}
			if (e.getActionCommand().equals("Sell Ore Mule")) {
				if (player.hasMule()
						&& player.getMule().toString().equals("Ore Mule")) {
					player.setMoney(player.getMoney() + ORE_MULE);
					player.setMule(null);
					numMule++;
					storeFoodMule.setText("Amount of total Mules: " + numMule);
					storeEnergyMule
							.setText("Amount of total Mules: " + numMule);
					storeOreMule.setText("Amount of total Mules: " + numMule);
					Game.bottomPanel.updateInfo();
				}
			}

		}

	}

	class StoreFrame extends JFrame {

		/**
		 * Sets up the store
		 */
		public StoreFrame() {
			setLayout(new GridLayout(0, 4));
			setTitle("Store");
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			setSize(850, 500);
			setVisible(true);
			setLocationRelativeTo(null);
			setResizable(false);
			//set initial store for beginner level
			if(Game.gameLevel == 1 && storeInitialized == false) {
				numFood = 16;
				numEnergy = 16;
				numOre = 0;
				numCrystite = 0;
				numMule = 25;
				storeInitialized = true;
			}
			
			//set initial store for standard and tournament level
			else if((Game.gameLevel == 2 || Game.gameLevel == 3) && storeInitialized == false){
				numFood = 8;
				numEnergy = 8;
				numOre = 8;
				numCrystite = 0;
				numMule = 14;
				storeInitialized = true;
			}
			storeFood = new JLabel("Amount of Food: " + numFood);
			foodPrice = new JLabel("Price: " + FOOD_PRICE);
			buyFood = new JButton("Buy 1 Food");
			sellFood = new JButton("Sell 1 Food");

			storeEnergy = new JLabel("Amount of Energy: " + numEnergy);
			energyPrice = new JLabel("Price: " + ENERGY_PRICE);
			buyEnergy = new JButton("Buy 1 Energy");
			sellEnergy = new JButton("Sell 1 Energy");

			storeOre = new JLabel("Amount of Ore: " + numOre);
			orePrice = new JLabel("Price: " + ORE_PRICE);
			buyOre = new JButton("Buy 1 Ore");
			sellOre = new JButton("Sell 1 Ore");

			storeCrystite = new JLabel("Amount of Crystite: " + numCrystite);
			crystitePrice = new JLabel("Price: " + CRYSTITE_PRICE);
			buyCrystite = new JButton("Buy 1 Crystite");
			sellCrystite = new JButton("Sell 1 Crystite");

			storeFoodMule = new JLabel("Amount of total Mules: " + numMule);
			foodMulePrice = new JLabel("Price: " + FOOD_MULE);
			buyFoodMule = new JButton("Buy Food Mule");
			sellFoodMule = new JButton("Sell Food Mule");

			storeEnergyMule = new JLabel("Amount of total Mules:  " + numMule);
			energyMulePrice = new JLabel("Price: " + ENERGY_MULE);
			buyEnergyMule = new JButton("Buy Energy Mule");
			sellEnergyMule = new JButton("Sell Energy Mule");

			storeOreMule = new JLabel("Amount of total Mules: " + numMule);
			oreMulePrice = new JLabel("Price: " + ORE_MULE);
			buyOreMule = new JButton("Buy Ore Mule");
			sellOreMule = new JButton("Sell Ore Mule");

			add(storeFood);
			add(foodPrice);
			add(buyFood);
			add(sellFood);
			add(storeEnergy);
			add(energyPrice);
			add(buyEnergy);
			add(sellEnergy);
			add(storeOre);
			add(orePrice);
			add(buyOre);
			add(sellOre);
			add(storeCrystite);
			add(crystitePrice);
			add(buyCrystite);
			add(sellCrystite);
			add(storeFoodMule);
			add(foodMulePrice);
			add(buyFoodMule);
			add(sellFoodMule);
			add(storeEnergyMule);
			add(energyMulePrice);
			add(buyEnergyMule);
			add(sellEnergyMule);
			add(storeOreMule);
			add(oreMulePrice);
			add(buyOreMule);
			add(sellOreMule);

			buyFood.addActionListener(new action());
			sellFood.addActionListener(new action());
			buyEnergy.addActionListener(new action());
			sellEnergy.addActionListener(new action());
			buyOre.addActionListener(new action());
			sellOre.addActionListener(new action());
			buyCrystite.addActionListener(new action());
			sellCrystite.addActionListener(new action());
			buyFoodMule.addActionListener(new action());
			sellFoodMule.addActionListener(new action());
			buyEnergyMule.addActionListener(new action());
			sellEnergyMule.addActionListener(new action());
			buyOreMule.addActionListener(new action());
			sellOreMule.addActionListener(new action());

		}

	}
}
